﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data.OleDb;
using System.Data;
using System.Data.Common;

namespace DataAccess
{
    public partial class MSAccessDataAccess : IDataAccess
    {
        #region IDataAccess Members
        /// <summary>
        /// Gets the List of Tables from database defined in ConnectionInformation Object.
        /// </summary>
        /// <param name="connectionInformation"></param>
        /// <returns></returns>
        public List<string> GetTableList(ConnectionInformation connectionInformation)
        {
            connection.ConnectionString = connectionInformation.getConnectionString();

            connection.Open();

            DataTable TableList = connection.GetSchema("Tables");

            for (int i = 0; i < TableList.Rows.Count; i++)
                if (TableList.Rows[i][2].ToString().StartsWith("MSys"))
                {
                    TableList.Rows.Remove(TableList.Rows[i]);
                    i--;
                }

            return GenerateTableList (TableList,2);
        }


        /// <summary>
        /// Get Information of a Table and return TableInformation Object.
        /// But it will retrieve only sample data in dataTable(DataTable).
        /// For getting full data call GetFullData(ConnectionInformation connectionInformation, string tableName,out TableInforamation tableInformation) after calling this function.
        /// </summary>
        /// <param name="connectionInformation"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public TableInformation GetTableInformation(ConnectionInformation connectionInformation, string TableName)
        {
            TableInformation tableInformation = new TableInformation(); ;

            string QueryString = "Select top 10 * from " + TableName;

            tableInformation.DataTable = ExecuteQuery(connectionInformation, QueryString);
            tableInformation.ColumnList = new List<Column>();
            int count = tableInformation.DataTable.Columns.Count;

            for (int i = 0; i < count; i++)
            {
                tableInformation.ColumnList.Add(new Column() { Name = tableInformation.DataTable.Columns[i].ColumnName, Type1 = tableInformation.DataTable.Columns[i].DataType.Name, IsImported = true });
            }
            
            tableInformation.DataTable.TableName = TableName;

            return tableInformation;
        }
        #endregion
    }
}

